#include "Socket.hpp"

int main() {
    wzh::Sock server;
    server.Socket();
    server.Bind(8080);
    server.Listen(5);
    int fd = server.Accept();
    char buffer[1024];

    while (1) {
        int sz = read(fd, buffer, sizeof(buffer) - 1);
        if (sz < 0) 
        {
            std::cout << "read failed\n";
            break;
        } 
        else if (sz > 0) 
        {
            buffer[sz] = '\0';  // 添加字符串结尾
            std::cout << "get : " << buffer << std::endl;

            // 发送响应
            int rsz = write(fd, buffer, sz);
            if (rsz < 0) 
            {
                std::cout << "write failed\n";
                break;
            }
            std::cout << "write : " << buffer << std::endl;
        } 
        else 
        {
            std::cout << "EOF, closed\n";
            break;
        }
    }

    return 0;
}
